package com.personal.design;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener , TextWatcher{

	private EditText code1;
	private EditText code2;
	private EditText code3;
	private EditText code4;
	private Button forgotBtn;
	private Button resetBtn;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		initUi();
	}

	private void initUi() {
		code1 = (EditText) findViewById(R.id.login_code_1_txt);
		code1.addTextChangedListener(this);
		code2 = (EditText) findViewById(R.id.login_code_2_txt);
		code2.addTextChangedListener(this);
		code3 = (EditText) findViewById(R.id.login_code_3_txt);
		code3.addTextChangedListener(this);
		code4 = (EditText) findViewById(R.id.login_code_4_txt);
		code4.addTextChangedListener(this);
		forgotBtn = (Button) findViewById(R.id.login_forgot_btn);
		forgotBtn.setOnClickListener(this);
		resetBtn = (Button) findViewById(R.id.login_reset_btn);
		resetBtn.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.login_forgot_btn:
			String str = code1.getText().toString() + "\t" +code2.getText().toString() + "\t" +code3.getText().toString() + "\t" +code4.getText().toString() ;
			Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
			break;
		case R.id.login_reset_btn:
			Toast.makeText(this, "Navigate", Toast.LENGTH_SHORT).show();
			Intent intent = new Intent(MainActivity.this, HomeActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}
	
	@Override
	public void afterTextChanged(Editable s) {
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		if(code1.hasFocus()) {
			updateFocus(null, code1, code2);
		} else if(code2.hasFocus()) {
			updateFocus(code1, code2, code3);
		} else if(code3.hasFocus()) {
			updateFocus(code2, code3, code4);
		} else if(code4.hasFocus()) {
			updateFocus(code3, code4, null);
		}
	}
	
	private void updateFocus(EditText prev, EditText cur, EditText next) {
		if(prev == null) {
			if(cur.getText().toString().length() > 0) 
				next.requestFocus();
		} else if(next == null) {
			if(cur.getText().toString().length() > 0) 
				validateAndNavigateToNextScreen();
			else
				prev.requestFocus();
		} else {
			if(cur.getText().toString().length() > 0)
				next.requestFocus();
			else 
				prev.requestFocus();
		}
	}

	private void validateAndNavigateToNextScreen() {
		
	}

}
